package com.awspaas.user.apps.sts;

import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.commons.at.impl.num.AbsExpression;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

public class IsTrustManagerNext extends AbsExpression {
    public IsTrustManagerNext(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    public String execute(String expression) {
        SDK.getLogAPI().consoleInfo("是否进入信托经理判断===");
        String flag="FALSE";

        String processId = getParameter(expression, 1);
        String projectCode = getParameter(expression, 2);
        SDK.getLogAPI().consoleInfo("项目编号==="+projectCode);
        String sql = "SELECT log.CALL_UID AS USERNO FROM BO_EU_INTERFACE_LOG log   WHERE log.TO_DO_PROCESSINSTID ='"+processId+"' AND  log.CALL_METHOD='TASKSYNC.startProcess' ORDER BY CALL_END_TIME DESC";
        // List<BO> maps = SDK.getBOAPI().query("BO_EU_INTERFACE_LOG").addQuery("TO_DO_PROCESSINSTID=",processID).desc().list();
        SDK.getLogAPI().consoleInfo("查询流程办理人NO的SQL==="+sql);
       String userNo = DBSql.getString(sql, "USERNO");
        SDK.getLogAPI().consoleInfo("查询流程办理人的userNO==="+userNo);
       if(null==userNo){
           return flag;
       }
        List<RowMap> rowMaps = DBSql.getMaps("SELECT USERNO,ROLENAME FROM BO_PS_TPROJECT_INFO_SYNC WHERE PROJECTCODE = ?  ", new Object[]{projectCode});
        SDK.getLogAPI().consoleInfo("获取项目人员的list==="+rowMaps);
        if(CollectionUtils.isEmpty(rowMaps)){
            return flag;
        }
        //信托经理
        List<RowMap> xtList = rowMaps.stream().filter(a -> a.getString("ROLENAME").indexOf("信托经理") >=0 && userNo.equals(a.getString("USERNO"))).collect(Collectors.toList());
        //运营经理
        List<RowMap> yyList = rowMaps.stream().filter(a -> a.getString("ROLENAME").indexOf("运营经理") >=0 && userNo.equals(a.getString("USERNO"))).collect(Collectors.toList());
        SDK.getLogAPI().consoleInfo("获取的运营经理的list==="+yyList);
        if(CollectionUtils.isEmpty(xtList)&&CollectionUtils.isEmpty(yyList)){
            return flag;
        }
        if(CollectionUtils.isNotEmpty(yyList)){//说明发起人是运营经理
            flag="TRUE";
        }else{
            return flag;
        }
        SDK.getLogAPI().consoleInfo("返回的是否信托经理flag==="+flag);
        return flag;

    }
}
